package guoanmaker.operator.business.controller;



import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

import guoanmaker.operator.business.model.OperatorLuckdraw;
import guoanmaker.operator.business.service.OperatorLuckdrawServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import guoanmaker.operator.helper.common.util.OSSUnitHelper;
import io.swagger.annotations.ApiOperation;

/**
 *  
 *
 * @author wanghongwei 
 * @ClassName: OperatorLuckdrawController
 * @Description: OperatorLuckdrawController 
 * @date 2017/8/14 下午17:28
 */
@RestController
@RequestMapping(ApiController.OPERATOR_LUCKDRAW)
public class OperatorLuckdrawController {
	private final OperatorLuckdrawServiceInterface  operatorLuckdrawServiceInterface;
	private static final Logger LOG = LoggerFactory.getLogger(OperatorLuckdrawController.class); 
	private OSSClient client = null;  
	
	  public void initUnit(){  
	        client = guoanmaker.operator.helper.common.util.OSSUnitHelper.getOSSClient();  
	   }  
	public OperatorLuckdrawController(OperatorLuckdrawServiceInterface  operatorLuckdrawServiceInterface){
		this.operatorLuckdrawServiceInterface=operatorLuckdrawServiceInterface;
	}
	 /** 
	    * @Title: saveOperatorLuckdraw
	    * @Description: 保存报名列表信息
	    * @param: OperatorLuckdrawController   
	    * @return AjaxJson    
	    * @throws 
	    */
	  @ApiOperation(value = "新增/修改奖项信息", notes = "新增/修改奖项信息")
	  @PostMapping(value = "saveOperatorLuckdraw")
	  public AjaxJson saveOperatorLuckdraw(@RequestBody OperatorLuckdraw operatorLuckdraw) {
		  AjaxJson ajaxJson=new AjaxJson();
		  return ajaxJson.setSuccess(true).setStatus(200).setData(operatorLuckdrawServiceInterface.saveOperatorLuckdraw(operatorLuckdraw));
	  }
	  /** 
	    * @Title: getLuckdrawListByActivityId
	    * @Description: 根据活动 用户 id查询奖项信息 及剩余次数
	    * @param: OperatorLuckdrawController   
	    * @return AjaxJson    
	    * @throws 
	    */
	  @ApiOperation(value = " 根据活动 用户 id查询奖项信息 及剩余次数", notes = " 根据活动 用户 id查询奖项信息 及剩余次数")
	  @PostMapping(value = "getLuckdrawListByActivityId")
	  public AjaxJson getLuckdrawListByActivityId(@RequestParam(value = "id") String id,@RequestParam(value = "userId") String userId) {
		  AjaxJson ajaxJson=new AjaxJson();
		  return ajaxJson.setSuccess(true).setStatus(200).setData(operatorLuckdrawServiceInterface.getLuckdrawListByActivityId(id,userId));
	  }
	  /** 
	    * @Title: getOperatorLuckdraw
	    * @Description: 抽奖方法
	    * @param: OperatorLuckdrawController   
	    * @return AjaxJson    
	    * @throws 
	    */
	  @ApiOperation(value = "抽奖方法", notes = "抽奖方法")
	  @PostMapping(value = "getOperatorLuckdraw")
	public AjaxJson getOperatorLuckdraw(@RequestBody OperatorLuckdraw operatorLuckdraw) {
		@SuppressWarnings("unused")
		AjaxJson ajaxJson = new AjaxJson();
		return ajaxJson = operatorLuckdrawServiceInterface.getOperatorLuckdraw(operatorLuckdraw);
	}
	/*  @ApiOperation(value = "抽奖方法", notes = "抽奖方法")
	  @PostMapping(value = "OperatorLuckdraw")
	public AjaxJson OperatorLuckdraw() {
		AjaxJson ajaxJson = null;
		return 	ajaxJson = operatorLuckdrawServiceInterface.OperatorLuckdraw();
	}*/
	  /** 
	    * @Title: UploadLuckDraw
	    * @Description: 抽奖图片上传接口
	    * @param: OperatorLuckdrawController   
	    * @return AjaxJson    
	    * @throws 
	    */
	  @ApiOperation(value = "抽奖图片上传接口", notes = "抽奖图片上传接口")
	  @PostMapping(value = "/UploadLuckDraw")
	  public AjaxJson UploadLuckDraw(MultipartFile upfile,HttpServletResponse response) { 
		  AjaxJson ajaxJson=new AjaxJson();
		  if(upfile!=null){
		  OSSClient client = OSSUnitHelper.getOSSClient(); 
		  String FileName=new Date().getTime()+(int)((Math.random()*9+1)*100000)+upfile.getOriginalFilename().substring(upfile.getOriginalFilename().indexOf("."));
		  System.out.println(FileName);
		  OSSUnitHelper.uploadObjectOSS(client, upfile,ApiController.LUCKDRAW_URL, FileName); 
		  String url=OSSUnitHelper.getUrl(client, ApiController.LUCKDRAW_URL,  FileName);
		  url = url.replace("http://", "https://");
		  if(url!=null&&!url.equals("")){
	        	ajaxJson.setSuccess(true).setStatus(200).setData(url);
	        	response.setContentType(url);
	        } 
		  }
		  return ajaxJson;
	  }

}
